package com.mine.consumer.controllers;



import com.mine.consumer.faign.faignclite;
import com.mine.consumer.model.Employee;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;

/**
 * @功能
 * @创建时间 2020/4/13
 */
@RestController
@RequestMapping("/ask")

public class AskController {
	//注入restTemplate对象
	@Autowired
	private RestTemplate restTemplate;

	/**
	 * 注入DiscoveryClient :
	 *  springcloud提供的获取原数组的工具类
	 *      调用方法获取服务的元数据信息
	 *
	 */
	@Autowired
	private DiscoveryClient discoveryClient;

	@Autowired
	private faignclite faignclite;

	@Autowired
	private RabbitTemplate amqpTemplate;


	@RequestMapping(value = "/put",method = RequestMethod.GET)
	public String put(String msg){
		CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
		System.out.println(msg);

		amqpTemplate.convertAndSend("amq.topic","topic.message1",msg);
		return "插入成功";
	}

	@RequestMapping(value = "/buy1/{id}",method = RequestMethod.GET)
	public Employee findById(@PathVariable Integer id) throws Exception {
		if(id != 1) {
			int i = 1/0;
			throw  new Exception("太忙了");
		}
		//调用discoveryClient方法
		//已调用服务名称获取所有的元数据
	///	List<ServiceInstance> instances = discoveryClient.getInstances("ability-service");
		//获取唯一的一个元数据
	//	ServiceInstance instance = instances.get(0);
		//根据元数据中的主机地址和端口号拼接请求微服务的URL
		//Employee product = null;
		//如何调用商品服务?
	//	product = restTemplate.getForObject("http://127.0.0.1"+":"+9011+"/ablivity/1",Employee.class);
		//		List<ServiceInstance> instances = discoveryClient.getInstances("service-product");

	//	product = restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/ablivity/1",Employee.class);
		//product = restTemplate.getForObject("http://ability-service/ablivity/1",Employee.class);
	//	product =faignclite.findById(id);
		return faignclite.findById(id);
	}

	@RequestMapping(value = "{id}",method = RequestMethod.GET)
	public String www(){
		return "好烦啊";
	}
}
